home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 184_01 / fl3.mac < prev    next >
Text File  |  1980-01-01  |  13KB  |  535 lines

  1. ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  2. ;
  3. ;This file contains the third of three patches to the 
  4. ;source code for FLTLIB to enable use of the AMD9511 
  5. ;(Intel 8231A) for floating point multiply/divide 
  6. ;and MATHLIB functions.
  7. ;N.T.Carnevale 8/84.
  8. ;
  9. ;
  10. ;See accompanying documentation.  Other useful information
  11. ;is in my article "Faster floating point math," which appeared
  12. ;on pp.46-54 of the Nov/Dec 1985 issue of Micro/Systems Journal.
  13. ;
  14. ;Use the following code to replace the source of the FLTLIB
  15. ;section of FLOATLIB.ASM from the line labelled fmult3: 
  16. ;through the line just before label pophrt:
  17. ;
  18.     ;
  19.     ;
  20. CR    EQU 0DH
  21. LF    EQU 0AH
  22. BDOS    EQU 5
  23. BOOT    EQU 0
  24. PSTRNG    EQU 9
  25.     ;
  26.     ;
  27.     ;send message to console
  28. PRMSG:
  29.     PUSH PSW
  30.     PUSH B
  31.     PUSH D
  32.     PUSH H
  33.     MVI C,PSTRNG
  34.     CALL BDOS
  35.     POP H
  36.     POP D
  37.     POP B
  38.     POP PSW
  39.     RET
  40.     ;
  41. ;**************************************************************
  42.     ;
  43.     ;
  44.     ;Port addresses
  45.     ;
  46. BASE    EQU 050H    ;base address of Compupro SS1 board
  47. CREG    EQU BASE+9    ;location of 9511's control & data ports
  48. DREG    EQU BASE+8
  49.     ;
  50.     ;
  51.     ;FPP error codes
  52.     ;
  53. ERRBITS EQU 1EH     ;the status register's error bits
  54. OVRFLO    EQU 2        ;overflow
  55. UNRFLO    EQU 4        ;underflow
  56. NEGARG    EQU 8        ;negative argument to sqrt or log function
  57. DIVZER    EQU 10H     ;divide by zero
  58. TOOBIG    EQU 18H     ;arg of asin, acos, or exp too big
  59.     ;
  60.     ;
  61.     ;FPP command codes
  62.     ;
  63. FMUL    EQU 12H
  64. FDIV    EQU 13H
  65. XCHF    EQU 19H     ;swap top two locations in fpp's stack
  66. FSQRT    EQU 1
  67. FSIN    EQU 2
  68. FCOS    EQU 3
  69. FTAN    EQU 4
  70. FASIN    EQU 5
  71. FACOS    EQU 6
  72. FATAN    EQU 7
  73. FLOG    EQU 8        ;log base 10
  74. FLN    EQU 9        ;natural logarithm
  75. FEXP    EQU 0AH     ;e^x
  76. FPWR    EQU 0BH     ;x^y
  77.     ;
  78. ;**************************************************************
  79. ;**************************************************************
  80. ;
  81. ;
  82. ;note:    timing indicated for some of the following
  83. ;
  84. ;This block converts c80 float in BD to FPP format, then loads it into FPP.  
  85. ;If out of range for FPP, aborts with warning.
  86. ;
  87. ;Data format conversion routine C2AMD--
  88. ;converts c80's float to amd's fp format.
  89. ;Based on suggestions by J.Shook, Electronics Lab, Dept.of Chemistry,
  90. ;SUNY Stony Brook
  91. ;
  92. ;Floating point formats
  93. ;----------------------
  94. ;C80 stores floats as:
  95. ;     mantissa sign in C7
  96. ;     mantissa  =  24 bit two's complement in CDE, 
  97. ;             with bit 23 assumed = 1.
  98. ;     exponent is added to 128 (80H) and stored in B.  
  99. ;             If the number is 0, B=0.
  100. ;
  101. ;FPP stores floats as:
  102. ;     mantissa sign in B7
  103. ;     mantissa  =  24 bit two's complement in CDE,
  104. ;             with bit 23 = 1.  However, 
  105. ;             the value 0 is represented by BD=0.
  106. ;     exponent sign in B6
  107. ;     exponent in B5-B0 (only 6 bits).
  108. ;
  109. ;
  110.     ;Call with value to convert in BD.
  111. C2AMD:
  112.     MOV A,B     ; 4
  113.     ORA A        ; 4
  114.     JZ FPPZERO    ;10--B=0 implies x=0
  115.     ;does exponent lie in FPP's range?
  116.     CPI 64+80H      ; 7
  117.     JNC EXPHI    ;10--exceeds 2^63
  118.     CPI -64+80H     ; 7
  119.     JC EXPLO    ;10--smaller than 2^-64
  120.     ;exponent ok, proceed
  121.     SUI 80H        ; 7--corrects exponent
  122.     ANI 7FH     ; 7--mask out hi bit of B
  123.     MOV B,A     ; 4-- and save til later
  124.     MOV A,C     ; 4
  125.     ANI 80H     ; 7--isolate mantissa sign bit
  126.     ORA B        ; 4--fix mantissa sign bit for FPP
  127.     MOV B,A     ; 4--done with B
  128.     ;take care of C
  129.     MVI A,80H    ; 7
  130.     ORA C        ; 4--set hi bit of C
  131.     MOV C,A     ; 4
  132.     JMP LOADFPP    ;10--put data in FPP
  133.     ;114 t cycles @ 4mHz = 28.5usec
  134.     ;
  135. FPPZERO:        ;x=0
  136.     XRA A
  137.     MOV C,A
  138.     MOV D,A
  139.     MOV E,A
  140.     ;a short cut, time not calculated
  141.     ;fall through to next routine
  142.     ;
  143.     ;
  144.     ;Data transfer routine
  145.     ;LOADFPP puts BD into fpp
  146. LOADFPP:
  147.     MOV A,E     ; 4
  148.     OUT DREG    ;11
  149.     MOV A,D     ; 4
  150.     OUT DREG    ;11
  151.     MOV A,C     ; 4
  152.     OUT DREG    ;11
  153.     MOV A,B     ; 4
  154.     OUT DREG    ;11
  155.     RET        ;10
  156.     ;70 t cycles = 17.5usec
  157.     ;
  158.     ;TOTAL TIME for C2AMD & LOADFPP = 46usec
  159.     ;
  160. ;**************************************************************
  161.     ;
  162.     ;
  163.     ;C2AMD found float to be out of range for FPP--warn & abort
  164. EXPHI:
  165.     LXI D,HIMSG
  166.     JMP ERREXIT
  167. EXPLO:
  168.     LXI D,LOMSG
  169. ERREXIT:
  170.     CALL PRMSG
  171.     JMP BOOT    ;bail out!
  172.     ;
  173.     ;
  174. HIMSG:    DB CR,LF,'float --> FPP exponent overflow ( > 2^63)$'
  175. LOMSG:    DB CR,LF,'float --> FPP exponent underflow ( < 2^-64)$'
  176.     ;
  177. ;**************************************************************
  178. ;**************************************************************
  179.     ;
  180.     ;
  181.     ;
  182.     ;FLD1 gets 1 float from the stack, puts it into fpp,
  183.     ;and DOES NOT restore stack.  Called by "intrinsic"
  184.     ;float arithmetic operations, e.g. fpmul and fpdiv.
  185. FLD1:
  186.     POP H        ;10--return addr for this block
  187.     SHLD RETADR    ;16
  188.     POP H        ;10--return addr for calling function
  189.     POP D        ;10
  190.     POP B        ;10--BD = argument
  191.     CALL C2AMD    ;17--convert format & load FPP
  192.     ;
  193.     PUSH H        ;11--restore return address of calling function
  194.     LHLD RETADR    ;16--return to calling function
  195.     PCHL        ; 4--  without disturbing stack
  196.     ;104 t cycles = 26usec, + 46 for C2AMD = 72usec
  197.     ;
  198. ;**************************************************************
  199.     ;
  200.     ;
  201.     ;FLOAD1 gets 1 float from the stack, puts it into fpp,
  202.     ;and restores stack for c80.  Called by "user-defined"
  203.     ;functions, not by fpmul or fpdiv.
  204. FLOAD1:
  205.     POP H        ;10--return addr for this block
  206.     SHLD RETADR    ;16
  207.     POP H        ;10--return addr for calling function
  208.     POP D        ;10
  209.     POP B        ;10--BD = argument
  210.     CALL C2AMD    ;17--convert format & load FPP
  211.     ;fix stack for c80
  212.     PUSH H        ;11
  213.     PUSH H        ;11
  214.     PUSH H        ;11--restore return address of calling function
  215.     LHLD RETADR    ;16--return to calling function
  216.     PCHL        ; 4--  without disturbing stack
  217.     ;126 t cycles = 31.5usec, + 46 for C2AMD = 77.5usec
  218.     ;
  219. ;**************************************************************
  220.     ;
  221.     ;
  222.     ;FLOAD2 gets 2 floats from the stack, puts them into fpp,
  223.     ;  and restores the stack for c80
  224. FLOAD2:
  225.     POP H        ;10--return addr for this block
  226.     SHLD RETADR    ;16
  227.     POP H        ;10--return addr for calling function
  228.     POP D        ;10
  229.     POP B        ;10--BD = second argument
  230.     CALL C2AMD    ;17--convert format & load FPP
  231.     POP D        ;10
  232.     POP B        ;10--BD = first argument
  233.     CALL C2AMD    ;17
  234.     ;fix stack for c80
  235.     PUSH H        ;11 x 5 = 55
  236.     PUSH H
  237.     PUSH H
  238.     PUSH H
  239.     PUSH H        ;restore return address of calling function
  240.     LHLD RETADR    ;16--return to calling function
  241.     PCHL        ; 4--  without disturbing stack
  242.     ;185 t cycles = 46.25usec, + 2 * 46 (for C2AMD) = 138.25usec
  243.     ;
  244. RETADR: DS 2        ;used by all FPP load routines
  245.     ;
  246. ;**************************************************************
  247. ;**************************************************************
  248.     ;
  249.     ;
  250.     ;
  251.     ;fpmask() allows the user to specify which error bits
  252.     ;are tested for error detection.  The argument to fpmask
  253.     ;becomes the ERRMASK that is ANDed with the status word 
  254.     ;to detect a hardware (FPP) error.
  255.  
  256.     PUBLIC fpmask
  257. fpmask: 
  258.     POP H        ;return address
  259.     POP B        ;argument
  260.     PUSH B        ;fix stack
  261.     PUSH H
  262.     MOV A,C     ;get the user-specified error mask
  263.     AN╔ ERRBIT╙    ╗ anΣ masδ ou⌠ al∞ bu⌠ thσ genuinσ erro≥ bits
  264.     STA ERRTEST+1
  265.     RET
  266.     ;
  267. ;**************************************************************
  268.     ;
  269.     ;
  270.     ;FPP Error handling routine
  271.     ;print message according to what sort of error occurred
  272. FPERR:
  273.     LXI D,EMSG0
  274.     CALL PRMSG    ;print general message
  275.     MOV C,A     ;save error code
  276.     MVI B,MSGPTR-ETYPES    ;# of codes to check
  277.     LXI H,ETYPES    ;M=first code in the list
  278.     LXI D,MSGPTR    ;(DE) = address that contains 
  279.             ;  location of message for first error code
  280. ERCHEK:
  281.     ANA M
  282.     JMP ERMATCH
  283.     MOV A,C     ;restore error code
  284.     INX H        ;advance to next code and message
  285.     INX D
  286.     INX D
  287.     DCR B
  288.     JNZ ERCHEK    ;more codes to test
  289.     ;should never fall through, but if it does, will print 'huh?'
  290.     ;
  291. ERMATCH:
  292.     XCHG        ;(HL) holds address of error message
  293.     MOV E,M
  294.     INX H
  295.     MOV D,M
  296.     JMP ERREXIT    
  297.     ;
  298.     ;
  299. EMSG0:    DB CR,LF,,LF,'FPP error:  $'
  300. EMSG1:    DB 'overflow$'
  301. EMSG2:    DB 'underflow$'
  302. EMSG3:    DB 'sqrt or log of negative number$'
  303. EMSG4:    DB 'divide by 0$'
  304. EMSG5:    DB 'argument of asin, acos, or exp too large$'
  305. EMSGX:    DB 'huh?$'    ;should never occur!
  306.     ;
  307.     ;
  308. ETYPES: DB OVRFLO,UNRFLO,TOOBIG,NEGARG,DIVZER
  309. MSGPTR: DW EMSG1,EMSG2,EMSG5,EMSG3,EMSG4,EMSGX
  310.     ;
  311. ;**************************************************************
  312. ;**************************************************************
  313.     ;
  314.     ;
  315.     ;Next come the hardware floating point routines themselves.
  316.     ;Thσ multiplcatio